package main

import (
	"github.com/veandco/go-sdl2/sdl"
)

const (
	W = 670
	H = 620
)

var (
	Gwindow  *sdl.Window
	Grender  *sdl.Renderer
	Gtexture *Texture
)

func init() {
	sdl.Init(sdl.INIT_EVERYTHING)
	Gwindow, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, W, H, sdl.WINDOW_SHOWN)
	if err != nil {
		panic(err)
	}
	Grender, err = sdl.CreateRenderer(Gwindow, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		panic(err)
	}
	Grender.SetDrawColor(128, 128, 128, 255)
	Gtexture = &Texture{}
	Gtexture.LoadFromFile("../asserts/image/texture.png")
}

func main() {
	running := true

	game := NewGame(Gtexture)

	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			}
		}
		Grender.SetDrawColor(255, 255, 255, 255)
		Grender.Clear()

		game.Event()
		game.Render()

		Grender.Present()
		sdl.Delay(16)
	}
	free()
}

func free() {
	Gtexture.Free()
	Grender.Destroy()
	Gwindow.Destroy()
	sdl.Quit()
}
